package com.house.rent.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.base.lib.mvp.present.BasePresenter;
import com.house.rent.App;
import com.house.rent.R;
import com.house.rent.entity.UserEntity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author neo.duan
 * @date 2019-09-11 20:05
 * @desc 注册
 */
public class RegisterActivity extends BaseActivity {

    @BindView(R.id.edit_reg_nickName)
    EditText mEdtNickName;
    @BindView(R.id.edit_reg_phone)
    EditText mEdtPhone;
    @BindView(R.id.edit_reg_pwd)
    EditText mEdtPwd;
    @BindView(R.id.edit_reg_pwd_repeat)
    EditText mEdtPwdRepeat;

    public static void start(Context context) {
        Intent starter = new Intent(context, RegisterActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void initTop() {
        setTitle("注册");
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initListeners() {

    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.btn_register)
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register:
                requestRegister();
                break;
            default:
                break;
        }
    }

    private void requestRegister() {
        String nickName = getNickName();
        String phone = getPhone();
        String pwd = getPwd();
        String repeatPwd = getRepeatPwd();

        if (TextUtils.isEmpty(nickName)) {
            showMessage("请输入用户名");
            return;
        }

        if (TextUtils.isEmpty(phone)) {
            showMessage("请输入手机号");
            return;
        }

        if (TextUtils.isEmpty(pwd)) {
            showMessage("请输入密码");
            return;
        }

        if (pwd.length() < 6 || pwd.length() > 15) {
            showMessage("请输入6~15位新密码");
            return;
        }

        if (TextUtils.isEmpty(repeatPwd)) {
            showMessage("请再次输入密码");
            return;
        }

        if (!pwd.equals(repeatPwd)) {
            showMessage("两次输入密码不一致");
            return;
        }

        showLoading("正在注册...");
        addParams("nickName", nickName);
        addParams("mobile", phone);
        addParams("password", pwd);
        sendRequest("user/register", UserEntity.class);
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        if (obj instanceof UserEntity) {
            showMessage("注册成功");

            //保存用户信息
            App.getInstance().saveUserEntity((UserEntity) obj);

            MainActivity.start(mContext);
        }
    }

    public String getNickName() {
        return mEdtNickName.getText().toString().trim();
    }

    public String getPhone() {
        return mEdtPhone.getText().toString().trim();
    }

    public String getPwd() {
        return mEdtPwd.getText().toString().trim();
    }

    public String getRepeatPwd() {
        return mEdtPwdRepeat.getText().toString().trim();
    }


}
